/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zwu.t2;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;
import javax.jcr.*;
import javax.jcr.nodetype.*;
import javax.jcr.query.*;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.query.Row;
import javax.jcr.query.RowIterator;
import org.apache.jackrabbit.core.TransientRepository;

/**
 *
 * @author Administrator
 */
public class ILookImportFileJoinAndCustomizedNode {

    public static void main(String[] args) throws Exception {
        Repository repository = new TransientRepository();
        Session session = repository.login(
                new SimpleCredentials("username", "password".toCharArray()));
        try {
            Node root = session.getRootNode();

            NodeTypeManager ntm = session.getWorkspace().getNodeTypeManager();
            NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();

            //ntt.setMixin(true);
            ntt.setName("newfile2");
            ntt.setDeclaredSuperTypeNames(new String[]{"nt:file", "nt:unstructured"});
            //ntt.setPrimaryItemName("nt:unstructured");
            ntm.registerNodeType(ntt, true);

            Node folder = root.addNode("filertf", "nt:folder");
            File file = new File("/zwu/docs/test1.txt");
            importFile(folder, file, "text/plain", "utf-8");
            session.save();

            // Retrieve content
            /*Node node = root.getNode("filetest/test1.txt/jcr:content");
            PropertyIterator pi = node.getProperties();
            while (pi.hasNext()) {
            System.out.println(pi.nextProperty().getName());
            }
            System.out.println(node.getProperties());
            System.out.println(node.getProperty("jcr:mimeType").getString());*/
            session.save();
            QueryManager qm = session.getWorkspace().getQueryManager();
            //String query = "select excerpt(.) from nt:resource where contains(., 'us')";
            //String query = "select * from nt:resource where contains(*, 'us')";
            String query = "select * from [nt:resource] as file inner join [newfile2] as parent on ischildnode(file, parent) where contains(file.*, 'zhou') and parent.title  like '%do%'";
            Query q = qm.createQuery(query, Query.JCR_SQL2);
            QueryResult result = q.execute();
            for (RowIterator it = result.getRows(); it.hasNext();) {
                Row r = it.nextRow();
                
                Node nn = r.getNode("file");
                PropertyIterator pi2 = nn.getProperties();
                while (pi2.hasNext()) {
                    System.out.println(pi2.nextProperty().getName());
                }
                for (int i = 0; i < r.getValues().length; i++) {
                    System.out.println(r.getValues()[i].getString());
                }
                //Value excerpt = r.getNode("file").g("rep:excerpt(file)");
                //System.out.print(excerpt.getString());
            }
            // Remove content

            root.getNode("filetest").remove();
            session.save();
        } finally {
            session.logout();
        }
    }

    public static void mynode(Workspace wk) throws Exception {
        NodeTypeManager ntm = wk.getNodeTypeManager();

        QueryManager qm = wk.getQueryManager();

        //Query q = qm.createQuery("select first from  mytag where first = 'zhou'", Query.SQL);
        Query q = qm.createQuery("select  from  [nt:file] where contains(first, 'zhou') and contains(first, 'w')", Query.JCR_SQL2);
        QueryResult result = q.execute();
        for (RowIterator it = result.getRows(); it.hasNext();) {
            Row r = it.nextRow();
            Value excerpt = r.getValue("first");
            System.out.println(excerpt.getString());
        }
    }

    public static Node importFile(Node folderNode, File file, String mimeType,
            String encoding) throws RepositoryException, IOException {
        //create the file node - see section 6.7.22.6 of the spec
        Node fileNode = folderNode.addNode(file.getName(), "newfile2");

        //create the mandatory child node - jcr:content
        Node resNode = fileNode.addNode("jcr:content", "nt:resource");
        resNode.setProperty("jcr:mimeType", mimeType);
        resNode.setProperty("jcr:encoding", encoding);
        resNode.setProperty("jcr:data", new FileInputStream(file));
        //Node metaNode = fileNode.addNode("meta");
        fileNode.setProperty("title", "do it");
        Calendar lastModified = Calendar.getInstance();
        lastModified.setTimeInMillis(file.lastModified());
        resNode.setProperty("jcr:lastModified", lastModified);

        return fileNode;
    }
}
